package org.thesis.activity;

import java.util.ArrayList;
import java.util.List;

import org.thesis.adapter.templates_sub_adapter;
import org.thesis.dataAccess.db_sub_templates;
import org.thesis.doituong.templates_sub;


import uit.mmt02.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;

public class sub_templatesActivity extends Activity 
{
	ListView listView;
	List<templates_sub>list; //list ds cac template duoc lay tu cdsl de dua vao adapter
	ArrayAdapter<templates_sub>adapter;
	db_sub_templates db;
	Cursor mCursor;
	String tag = "sub_tempActivity";
	int requestCode = 1;
	final static String KEY_NEW_SUB_TEMP = "new_sub_temp";
	@Override
	//coder : nguyen van khoa , bkdn, vankhoa0401@gmail.com 
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sub_templates);
		listView = (ListView) findViewById(R.id.lv_sub);
		list = new ArrayList<templates_sub>();
		//kiem tra neu csdk trong thi insert vao
		db = new db_sub_templates(this);
		db.open();
		mCursor = db.query();
		if(!mCursor.moveToFirst()) //neu ko co csdl thi insert
		{
			Log.v(tag, "ko co csdl, bat dau insert");
			db.insertDB();
			mCursor.close();
			db.close();
			db.open();
			mCursor = db.query();
		}else
			Log.v(tag, "da co csdl...");
		
		Log.v(tag, "doc csdl ");
		if(mCursor.moveToFirst())
		{
			do
			{
				int stt = mCursor.getInt(0);
				String mess = mCursor.getString(1);
				String tag = mCursor.getString(2);
				list.add(new templates_sub(stt, mess, tag));
				Log.v(tag, "add thanh cong vao list  " + mess);
			}while(mCursor.moveToNext());
		}
		mCursor.close();
		db.close();
		adapter = new templates_sub_adapter(this, list);
		listView.setAdapter(adapter);
	}
	
	public void img_onClick(View v)
	{
		ImageButton img = (ImageButton)v;
		switch (img.getId()) {
		case R.id.img_subtemp_backA:
			finish();
			break;
		case R.id.img_subtemp_menu:
			openOptionsMenu();
			break;

		default:
			break;
		}
	}
	
	//menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.menu_sub_temp, menu);
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.item_subtemp_add:
//			templates_sub sub = new templates_sub(list.size() + 1, "nguyen van khoa", tag);
//			list.add(sub);
//			adapter.notifyDataSetChanged();
//			//add vao csdl
//			db.open();
//			db.insert(sub);
//			db.close();
//			Log.v(tag, "add");
			Intent i = new Intent(this, org.thesis.activity.addsubtempActivity.class);
			
			startActivityForResult(i, requestCode);
			break;
		case R.id.item_subtemp_refresh:
			Log.v(tag, "refresh");
			list = new ArrayList<templates_sub>();
			adapter.notifyDataSetChanged();
//			db.open();
//			mCursor = db.query();
//			if(mCursor.moveToFirst())
//			{
//				do
//				{
//					int stt = mCursor.getInt(0);
//					String mess = mCursor.getString(1);
//					String tag = mCursor.getString(2);
//					list.add(new templates_sub(stt, mess, tag));
//					Log.v(tag, "add thanh cong vao list  " + mess);
//					adapter.notifyDataSetChanged();
//				}while(mCursor.moveToNext());
//			}
//			mCursor.close();
//			db.close();
//			adapter.notifyDataSetChanged();
			break;

		default:
			break;
		}
		return super.onMenuItemSelected(featureId, item);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case RESULT_OK:
			String s = data.getStringExtra(KEY_NEW_SUB_TEMP);
			if(s.length() > 0)
			{
				templates_sub sub = new templates_sub(list.size() + 1, s, tag);
				list.add(sub);
				adapter.notifyDataSetChanged();
				//add vao csdl
				db.open();
				db.insert(sub);
				db.close();
			}
			break;

		default:
			break;
		}
	}
}
